package com.hmall.user.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.domain.R;
import com.hmall.user.domain.dto.LoginFormDTO;
import com.hmall.user.domain.dto.SetUserRoleDTO;
import com.hmall.user.domain.po.SysUser;
import com.hmall.user.domain.vo.UserLoginVO;
import com.hmall.user.domain.vo.UserPageQueryVO;
import com.hmall.user.service.SysUserRolesService;
import com.hmall.user.service.SysUserService;
import com.hmall.user.utils.JwtTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final SysUserService userService;
    private final SysUserRolesService sysUserRolesService;
    private final JwtTool jwtTool;

    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public UserLoginVO login(@RequestBody @Validated LoginFormDTO loginFormDTO){
        return userService.login(loginFormDTO);
    }

    @ApiOperation("扣减余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pw", value = "支付密码"),
            @ApiImplicitParam(name = "amount", value = "支付金额")
    })
    @PutMapping("/money/deduct")
    public void deductMoney(@RequestParam("pw") String pw,@RequestParam("amount") Integer amount){
        userService.deductMoney(pw, amount);
    }
    @GetMapping("/checkToken")
    public String checkToken(@RequestHeader("token") String token){
        jwtTool.parseToken(token);
        return "ok";
    }
    /**
     * 分页获取用户信息
     * @param vo
     * @Return: com.hmall.common.domain.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.hmall.user.domain.po.SysUser>>
     * @author: yangfeng
     * @date: 2024/12/17 16:53
     **/
    @PostMapping(value = "/getUserPageData",name = "分页获取用户信息")
    public R<Page<SysUser>> getUserPageData(@RequestBody UserPageQueryVO vo){
        return R.ok(userService.getUserPageData(vo));
    }
    /**
     * 用户设置角色
     * @param dto
     * @Return: com.hmall.common.domain.R<java.lang.Boolean>
     * @author: yangfeng
     * @date: 2024/12/19 16:15
     **/
    @PostMapping(value = "/setUserRole",name = "设置用户角色")
    public R<Boolean> setUserRole(@RequestBody SetUserRoleDTO dto){
        return R.ok(sysUserRolesService.setUserRole(dto));
    }
}

